import { _decorator, Component, director, Label, Node, resources, Sprite, tween, UIOpacity, } from 'cc';
const { ccclass, property } = _decorator;
import {TimeBasedRequest} from "./Tool/dataUpdater";
import { ReactiveAutoSave } from './Tool/ReactiveAutoSave';
import { Config } from './Config';

@ccclass('loading')
export class loading extends Component {
    readonly config: string[] = [
        "card3",
        "music",
        "sound"
    ]
    @property(Sprite)
    progress: Sprite = null!;

    @property(Label)
    progressLabel: Label = null!;

    @property(UIOpacity)
    uiOpacity: UIOpacity = null!;
    
    @property(Node)
    mainAction: Node = null!;
   
    @property({type:Node})
    public ErrorShow = null!;    // 错误提示
    isUpdate: boolean = false;
    count: number = 0;
   
 
   
    start() {
 // 初始化进度条
 
         ReactiveAutoSave.init("gameData",{gift:0, level:1,userStatus:0})
        this.progress.fillRange = 0;
        this.progressLabel.string = "加载资源中..0%";
        this.mainAction.active = false;
        if(Config.loading){
            this.node.active = false;
            this.mainAction.active = true
        }else{
            this.node.active = true ;
            this.mainAction.active = false;
        }
        // let UIOpacityS =this.node.getComponent(UIOpacity)
        // console.log(this.node.getComponent(UIOpacity))
        // UIOpacityS.opacity = 100;
    }

    /** 加载资源 */
    preLoad() {
        if (this.count < this.config.length) {
            resources.loadDir(this.config[this.count], (hasLoad, all) => { }, (err, data) => {
                this.count++;
                this.isUpdate = false;
            });
        } else {
            this.progressLabel.string = "进入游戏中...";
            this.hid();
        }
    }

    hid(){
       tween(this.node)
       .delay(0.1)
      .call(() => {
        this.onDataUpdater()
      })
      .start();
    }

    async onDataUpdater(){
        await TimeBasedRequest.checkAndRequestData(4, 0, 5, 0).then( (status: number | boolean): void => {
			//1.上架审核中，2.审核通过，3.关闭客户小程序 // 晚上 4:00 到 5:00
			if (status && status != 3 ) {
				ReactiveAutoSave.ins.getData().userStatus = status
			}else if(status == 3){
					ReactiveAutoSave.ins.getData().userStatus = 3
			 }
			if (status === 3 || ReactiveAutoSave.ins.getData().userStatus == 3) {
				console.log("isgame:" + ReactiveAutoSave.ins.getData().userStatus);
                this.ErrorShow.active = true;
                return;
			} else {
                this.node.active = false;
                this.mainAction.active = true;
                this.node.destroy()
			}
		});
    }


    update(deltaTime: number) {
        if (this.isUpdate) { return; }
        let temp = this.progress.fillRange + 0.02;
        this.progress.fillRange = temp / 1;
        this.progressLabel.string = "加载资源中..." + Math.round(this.progress.fillRange * 100) + "%";
        if (temp > this.count / this.config.length) {
            this.isUpdate = true;
            this.preLoad();
        }
    }
}


